import time

def perf_time(func):
    def decorator(func):
        start = time.time()
        func()
        end = time.time()
        total_time = '%.2f'%(end - start)
        print(f"执行{func.__name__}用时：{total_time}")
    return decorator(func)

# @perf_time
# def foo():
#     for i in range(1000):
#         for j in range(100000):
#             pass
#     print('OK')
#     return "OK"

# 返回当前的时间戳（1970以后）
print(time.time())
# 格式化时间戳，获得时间元组
print(time.localtime(time.time()))  #
#output：time.struct_time(tm_year=2018, tm_mon=8, tm_mday=31, tm_hour=14, tm_min=27, tm_sec=46, tm_wday=4, tm_yday=243, tm_isdst=0)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))  # 格式化自己想输出的形式
#output：2018-08-31 14:27:46

import datetime
print(datetime.datetime.now())
#格式化输出
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print(datetime.datetime.today())
start = datetime.datetime.now()
while True:
    for i in range(100000000):
        pass
    break
end = datetime.datetime.now()
print("程序运行时间："+str((end-start).seconds)+"秒")
